<template>
	<view>
		<van-swipe-cell :async-close="true" id="swipe-cell" class="swiperCell" @close="onClose" :right-width="70" :name="index"  v-for="(item,index) in addressList" :key="index">
			<view class="out-side-box flex-culomn" style="margin-top: 20rpx;" @click="selectAddress(item.id)">
				<view class="left-box flex-just">
					<text class="name-width fontside-16">{{item.name}}</text>
					<text class="common-width fontside-16">{{item.phone}}</text>
					<text class="default-width fontside-14" v-if="item.type == 1">默认地址</text>
					<text class="default-width fontside-14" v-if="item.type == 0"></text>
				</view>
				<view class="flex-row">
					<view class="left-box flex-just">
						<text class="name-width fontside-14">{{item.school}}</text>
						<text class="address-width fontside-14">{{item.address}}</text>
					</view>
					<image src="/static/center/userInfo/baifangjiluS.png" class="right-icon" @click.stop="turnUpdateAddress(item.id)"></image>
				</view>
			</view>
			<view class="delete" slot="right">
				<text>删除</text>
			</view>
		</van-swipe-cell>
		
		<view class="width-90">
			<button class="button-polution common-button" @click="getAddressNum()">新建收货地址</button>
		</view>
	</view>
</template>

<script>
	import {
		getAddressNum,
		getDeliveryAddress,
		deleteAddress
	} from '@/common/api/center/address.js'
	export default {
		data() {
			return {
				isGetAddress: false,
				addressList: [],
				// 判断是否从订单页面跳过来,默认不是
				selectStatus: false
			}
		},
		methods: {
			// 统计地址是否超出限制
			getAddressNum:function() {
				getAddressNum(uni.getStorageSync("memberId")).then(res => {
					if(res.data.code == 200) {
						if(res.data.data == false) {
							// 地址数还没到5个时可以跳转到添加地址
							uni.navigateTo({
								url: '/pages/center/deliveryAddress/insertAddress/insertAddress'
							})
						} else {
							uni.showToast({
								title: '地址数目已超过5个',
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: "网络错误",
							icon: 'none'
						})
					}
				})
			},
			// 获取地址列表
			getDeliveryAddress:function(){
				getDeliveryAddress(uni.getStorageSync("memberId")).then(res => {
					if(res.data.code == 200) {
						if(res.data.data != null) {
							this.addressList = res.data.data
						} 
					} else {
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						})
					}
				})
			},
			// 点击删除
			onClose(event) {
				// name为第几个滑动单元格的标志
				var that = this
				const {
					position,
					instance,
					name
				} = event.detail;
				switch (position) {
					case 'cell':
						instance.close();
						break;
					case 'right':
						uni.showModal({
							// title: '提示',
							content: '确定删除吗？',
							success: function(res) {
								if (res.confirm) {
									that.deleteAddress(name)
								}
								instance.close();
							}
						})
						break;
				}
			},
			// 删除收获地址
			deleteAddress:function(num){
				let addressId = 0
				this.addressList.forEach(function(item,index) {
					if(index == num){
						addressId = item.id
						return
					}
				})
				console.log(addressId)
				deleteAddress(addressId).then(res => {
					if(res.data.code == 200) {
						uni.showToast({
							title: "删除成功",
							icon: 'none'
						})
						this.getDeliveryAddress()
					} else{
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						})
					}
				})
			},
			// 跳转到编辑地址页面
			turnUpdateAddress:function(addressId){
				uni.navigateTo({
					url: '/pages/center/deliveryAddress/insertAddress/insertAddress?addressId='+ addressId
				})
			},
			// 从订单页面过来的选择某个地址
			selectAddress:function(addressId){
				if(this.selectStatus){
					uni.navigateTo({
						url: '/pagesA/pages/groceryStore/orderDetial/orderDetial?addressId=' + addressId
					})
				}
				
			},
		},
		onLoad(e) {
			this.getDeliveryAddress()
			this.selectStatus = e.select
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
	}

	.left-box {
		width: 90%;
	}

	.right-icon {
		width: 40rpx;
		height: 40rpx;
	}

	.row-height {
		width: 100%;
		height: 60rpx;
	}
	.name-width{
		 width: 25%;
	}
	.common-width {
		width: 40%;
	}

	.default-width {
		width: 20%;
	}

	.address-width {
		width: 70%;
	}
	
</style>
